import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  build: {
    // 启用 CSS 代码分割
    cssCodeSplit: true,
    // 启用源码映射
    sourcemap: false,
    // 配置 Rollup 打包选项
    rollupOptions: {
      output: {
        // 分包策略
        manualChunks: {
          'react-vendor': ['react', 'react-dom'],
          'utils': ['@/utils/helpers.ts'],
          'icons': ['lucide-react']
        },
        // 自定义 chunk 文件名格式
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
      }
    },
    // 配置压缩选项
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    },
    // 设置打包文件大小警告阈值
    chunkSizeWarningLimit: 500
  },
  // 优化依赖预构建
  optimizeDeps: {
    exclude: ['lucide-react'],
    include: ['react', 'react-dom']
  }
})